﻿using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using SymulatorEthernetWPF.SimObjects.Devices;
using SymulatorEthernetWPF.UserInterface;
using SymulatorEthernetWPF.Shared;
using System.Windows.Documents;

namespace SymulatorEthernetWPF.UserInterface.UIElements
{
    public static class InfoBar
    {
        private static readonly Label InfoBarDevName = StaticVars.MainWindow.deviceName;
        private static readonly Image InfoBarDevImage = StaticVars.MainWindow.deviceImage;
        private static readonly Label InfoBarDeviceType = StaticVars.MainWindow.deviceType;
        private static readonly Label InfoBarNetworkInt1Info = StaticVars.MainWindow.networkInt1Info;
        private static readonly Label InfoBarNetworkInt2Info = StaticVars.MainWindow.networkInt2Info;
        private static readonly Label InfoBarNetworkInt3Info = StaticVars.MainWindow.networkInt3Info;
        private static readonly Label InfoBarNetworkInt4Info = StaticVars.MainWindow.networkInt4Info;
        private static readonly Label InfoBarNetworkInt5Info = StaticVars.MainWindow.networkInt5Info;
        private static readonly Label InfoBarNetworkInt6Info = StaticVars.MainWindow.networkInt6Info;
        private static readonly Label InfoBarNetworkInt7Info = StaticVars.MainWindow.networkInt7Info;
        private static readonly Label InfoBarNetworkInt8Info = StaticVars.MainWindow.networkInt8Info;
        private static readonly Border InfoBarScrollviewInfoBorder = StaticVars.MainWindow.scrollviewInfoBoxBorder;
        private static readonly RichTextBox LogRichTextBox = StaticVars.MainWindow.LogRichText;

        public static DeviceControl deviceControl
        {
            set
            {
                if (value == null)
                {
                    InfoBarScrollviewInfoBorder.Visibility = Visibility.Hidden;

                    InfoBarDevImage.Source = (ImageSource)(new ImageSourceConverter()).ConvertFromString("pack://application:,,/UserInterface/Images/infoI.png");
                    InfoBarDeviceType.Visibility = Visibility.Hidden;
                    LogRichTextBox.Visibility = Visibility.Hidden;
                    InfoBarDevName.Content = "Zaznacz urządzenie aby wyświetlić informacje";
                }
                else
                {
                    InfoBarScrollviewInfoBorder.Visibility = Visibility.Visible;
                    InfoBarDeviceType.Visibility = Visibility.Visible;
                    LogRichTextBox.Visibility = Visibility.Visible;
                    InfoBarDevName.Content = value.GetDeviceLabel();
                    if (value.GetType() == typeof (HostControl))
                    {
                        InfoBarDeviceType.Content = "Host";
                        StaticVars.MainWindow.scrollviewInfoBoxBorder.Visibility = Visibility.Visible;
                    }
                    else if (value.GetType() == typeof (HubControl))
                    {
                        InfoBarDeviceType.Content = "Hub";
                        StaticVars.MainWindow.scrollviewInfoBoxBorder.Visibility = Visibility.Collapsed;
                    }
                    else if (value.GetType() == typeof (SwitchControl))
                    {
                        InfoBarDeviceType.Content = "Switch";
                        StaticVars.MainWindow.scrollviewInfoBoxBorder.Visibility = Visibility.Visible;
                    }
                    InfoBarDevImage.Source = value.icon.Source;
                    if(value.GetType()!=typeof(HubControl))
                    {
                        InfoBarNetworkInt1Info.Content = "[" + value.interfaces[0].Name + "]@" + value.interfaces[0].Adress.Adress;
                        InfoBarNetworkInt2Info.Content = "[" + value.interfaces[1].Name + "]@" + value.interfaces[1].Adress.Adress;
                        InfoBarNetworkInt3Info.Content = "[" + value.interfaces[2].Name + "]@" + value.interfaces[2].Adress.Adress;
                        InfoBarNetworkInt4Info.Content = "[" + value.interfaces[3].Name + "]@" + value.interfaces[3].Adress.Adress;
                        InfoBarNetworkInt5Info.Content = "[" + value.interfaces[4].Name + "]@" + value.interfaces[4].Adress.Adress;
                        InfoBarNetworkInt6Info.Content = "[" + value.interfaces[5].Name + "]@" + value.interfaces[5].Adress.Adress;
                        InfoBarNetworkInt7Info.Content = "[" + value.interfaces[6].Name + "]@" + value.interfaces[6].Adress.Adress;
                        InfoBarNetworkInt8Info.Content = "[" + value.interfaces[7].Name + "]@" + value.interfaces[7].Adress.Adress;
                    }

                    FlowDocument log = new FlowDocument();
                    Paragraph para = new Paragraph();
                    foreach (string it in value.logList)
                    {
                        para.Inlines.Add(new Run(it + "\n"));
                    }
                    log.Blocks.Add(para);
                    LogRichTextBox.Document = log;
                }
            }
        }
    }
}